/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sk.stancel.peter.sudoku;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import sk.stancel.peter.sudoku.core.Field;
import sk.stancel.peter.sudoku.core.SudokuCheck;
import android.app.Application;
import android.graphics.Typeface;
import android.os.Environment;
import android.widget.Toast;

/**
 * Main class.
 * @author Peter Stancel
 */
public final class Sudoku extends Application{

    private SudokuCheck sudokuCheck;
    private static boolean easyMode = true;
    private static Field field;
    private static Typeface font;

    @Override
    public void onCreate() {
    	super.onCreate();

    	font = Typeface.createFromAsset(getAssets(), "font.ttf");
    }

    /**
     * Starts new game.
     */
    public Field newGame() {
    	field = new Field();
//        Thread thread = new Thread(sudokuCheck = new SudokuCheck(field));
//        thread.start();
        return field;
    }

    /**
     * Starts new game.
     * @param ints ints representing numbers in field
     */
    public Field customNewGame(int[] ints, boolean isHard) {
        field = new Field(ints);
        if(!isHard){
        	Thread thread = new Thread(sudokuCheck = new SudokuCheck(field));
            thread.start();
        }
        return field;
    }

     /**
      * Starts loaded game.
      * @param field field of sudoku
      */
    public Field loadNewGame(Field field, boolean isHard) {
    	if(field != null){
    		Sudoku.field = field;
    		if(!isHard){
    			Thread thread = new Thread(sudokuCheck = new SudokuCheck(field));
                thread.start();
    		}
    	}
        return field;
    }

	public boolean saveSudoku(String text, boolean bool) {
		File folder = new File(Environment.getExternalStorageDirectory(), "Sudoku");
		folder.mkdir();
		File file = new File(folder, text + ".sud");
		try {
			if (!bool && file.exists()) {
				return false;
			}
			FileOutputStream fis = new FileOutputStream(file);
            ObjectOutputStream s = new ObjectOutputStream(fis);

            s.writeObject(field);
            fis.close();
            s.close();
		} catch (IOException e) {
			Toast.makeText(this, getString(R.string.exception_saving) + e.getMessage(), Toast.LENGTH_SHORT).show();
		}
		return true;
	}
	
	public Field loadSudoku(File file) {
		Field newField = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);

            newField = (Field) ois.readObject();

            loadNewGame(newField, false);
            
            ois.close();
            fis.close();
        } catch (IOException e) {
        	Toast.makeText(this, R.string.exception_no_sudoku, Toast.LENGTH_SHORT).show();
        } catch (ClassNotFoundException e) {
        	Toast.makeText(this, getString(R.string.exception_loading) + e.getMessage(), Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
        	Toast.makeText(this, getString(R.string.exception_loading) + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
        return newField;
	}

    /**
     * Checks how many solutions has Sudoku.
     * @return the sudokuCheck
     */
    public SudokuCheck getSudokuCheck() {
        return sudokuCheck;
    }

	public boolean isEasyMode() {
		return easyMode;
	}

	public void setEasyMode(boolean easyMode) {
		Sudoku.easyMode = easyMode;
	}

	public static int getBackgroundImageID(int position, int tileValue) {
		int image = 1;
	    if ((position < 27 && position > 17) || (position < 54 && position > 44)) {
	    	image += 1;
	    }
	    if ((((position+1) % 3) == 0) && ((position+1)/3) % 3 != 0) {
	    	image += 2;
	    }
	    int row = position / 9;
		int column = position % 9;
	    if(easyMode && tileValue != 0){
	    	
	    	//Log.i("SUDOKU",field +"-field  "+ row+"-row , column-"+column+"  "+field.getTempTileValue(row, column)+" aassdd");
	    	if(field.getTempTileValue(row, column) == tileValue){
		    	image += 4;
	    	}else image += 8;
	    }
	    switch (image) {
		case 1:
			image = R.drawable.n;
			break;
		case 2:
			image = R.drawable.b;
			break;
		case 3:
			image = R.drawable.r;
			break;
		case 4:
			image = R.drawable.br;
			break;
		case 5:
			image = R.drawable.g_n;
			break;
		case 6:
			image = R.drawable.g_b;
			break;
		case 7:
			image = R.drawable.g_r;
			break;
		case 8:
			image = R.drawable.g_br;
			break;
		case 9:
			image = R.drawable.r_n;
			break;
		case 10:
			image = R.drawable.r_b;
			break;
		case 11:
			image = R.drawable.r_r;
			break;
		case 12:
			image = R.drawable.r_br;
			break;
		}
		return image;
	}

	public static Typeface getFont() {
		return font;
	}
	
	public void setField(Field field){
		Sudoku.field = field;
	}
}